
<template>
  <div class="password" id="password">
    <div class="top">
      <img class="img1" src="../../../wechat/static/images/login_bg.png" alt>
      <img class="img2" src="../../../wechat/static/images/login_logo.png" alt>
    </div>
    <div class="middle">
      <div class="two">
        <div class="section">
          <div class="img">
            <img src="../../../wechat/static/images/login_zh.png" alt>
          </div>
          <input type="text" placeholder="请输入账号" v-model="userName">
        </div>
        <div class="section">
          <div class="img">
            <img src="../../../wechat/static/images/login_password.png" alt>
          </div>
          <input v-model="pwd" type="password" placeholder="请输入密码">
        </div>
      </div>
      <div class="three">
        <a href="javascript:;" @click="login">登录</a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "password",
  data() {
    return {
      userName: "",
      pwd: "",
    };
  },
  mounted() {
    console.log(this.$route.query,'this.$route.query')
    if (this.$route.query.type == "store") {
      // 门店商户登录
      this.$common.setCookie('sessionId',this.$common.getCookie('sessionIdstore')||'')
      // localStorage.setItem("userType", "store");
      if (this.$common.getCookie("sessionId")&&this.$common.getCookie("sessionId")!='null') {
        this.userName = localStorage.getItem('userNamestore')||''
        this.pwd = localStorage.getItem('pwdstore')||''
        localStorage.setItem("storeName", '');
        // this.$router.push("./code");
      }else {
        this.userName = localStorage.getItem('userNamestore')||''
        this.pwd = localStorage.getItem('pwdstore')||''
      }
    } else {
      this.$common.setCookie('sessionId',this.$common.getCookie('sessionIdspecial')||'')
      // localStorage.setItem("userType", "special");  
      if (this.$common.getCookie("sessionId")&&this.$common.getCookie("sessionId")!='null') {
        this.userName = localStorage.getItem('userNamespecial')||''
        this.pwd = localStorage.getItem('pwdspecial')||''
        // this.findEsShopUserCoreById()
      }else {
        this.userName = localStorage.getItem('userNamespecial')||''
        this.pwd = localStorage.getItem('pwdspecial')||''
      }
    }
  },
  beforeCreate(){
    // if (this.$route.query.store == "store"){
    //   localStorage.setItem('userType','store')
    // }else {
    //   localStorage.setItem('userType','special')
    // }
    // if(this.$common.getQueryString('type')=='phone'){
    //   if(this.$common.isIOS()){
    //     window.location.href =API+ '?type=phone#/phone'
    //   }else {
    //     this.$router.push('/phone')
    //   }
    // }
  },
  created() {},
  methods: {
    findEsShopUserCoreById() {
      let url =
        this.$CONSTANT.MY.findEsShopUserCoreById +
        "?sessionId=" +
        this.$common.getCookie("sessionId");
      let param = {};
      this.$vux.loading.show({
        text: "自动登录中"
      });
      this.$common.post(url, param, res => {
        setTimeout(() => {
          this.$vux.loading.hide();
        }, 800);
        if (res.status == 200) {
          var bussData = res.data.bussData;
          localStorage.setItem("userId", bussData.userId);
          if(bussData.type=='special'){
            setTimeout(() => {
              let openId = localStorage.getItem("openId");
              this.saveEsShopUserCore(bussData.userId,openId);
            }, 2000);
            this.$router.push("./homeC");
          }else {
            localStorage.setItem('company','')
            this.userName = localStorage.getItem('userNamespecial')||''
            this.pwd = localStorage.getItem('pwdspecial')||''
          }
        } else {
          this.userName = localStorage.getItem('userNamespecial')||''
          this.pwd = localStorage.getItem('pwdspecial')||''
          this.$vux.toast.show({
            text: res.msg,
            type: "warn",
            width: "auto"
          });
        }
      });
    },
    login() {
      if (!this.userName) {
        this.$vux.toast.show({
          text: "账号不能为空",
          type: "warn",
          width: "auto"
        });
        return;
      }
      if (!this.pwd) {
        this.$vux.toast.show({
          text: "密码不能为空",
          type: "warn",
          width: "auto"
        });
        return;
      }
      if (this.$route.query.type == "store") {
        // 门店商户登录
        var url = this.$CONSTANT.STORE.login;
        var param = {
          pwd: this.pwd,
          userName: this.userName
        };
      } else {
        // 定制用户登录
        var url = this.$CONSTANT.MY.speciallogin;
        var param = {
          pwd: this.pwd,
          userName: this.userName,
          type: "special"
        };
      }

      this.$vux.loading.show({
        text: "登录中"
      });
      setTimeout(() => {
        this.$common.post(url, param, res => {
          this.$vux.loading.hide();
          if (res.status == 200) {
            var bussData = res.data.bussData;
            this.$vux.toast.show({
              text: res.msg,
              type: "",
              width: "auto"
            });

            setTimeout(() => {
              this.$common.setCookie("sessionId", bussData.sessionId);
              localStorage.setItem("userId", bussData.userId);
              if (this.$route.query.type == "store") {
                // 门店商户登录成功
                this.$common.setCookie("sessionIdstore", bussData.sessionId);
                localStorage.setItem("userNamestore", this.userName);
                localStorage.setItem("pwdstore", this.pwd);
                localStorage.setItem("storeName", '');
                localStorage.setItem("loginsuccesstype", 'store');
                localStorage.setItem("storeP", this.pwd);
                this.$router.push("./code");
              } else {
                // 定制用户登录成功
                localStorage.setItem('company','')
                this.$common.setCookie("sessionIdspecial", bussData.sessionId);
                localStorage.setItem("userNamespecial", this.userName);
                localStorage.setItem("pwdspecial", this.pwd);
                localStorage.setItem("loginsuccesstype", 'special');
                let openId = localStorage.getItem("openId");
                this.saveEsShopUserCore(bussData.userId,openId);
                this.$router.push("./homeC");
              }
              this.$common.isOnloadPage()
            }, 1000);
          } else {
            this.$vux.toast.show({
              text: res.msg,
              type: "warn",
              width: "auto"
            });
          }
        });
      }, 1000);
    },
    saveEsShopUserCore(userId,openId) {
      if (location.href.includes("localhost")) return;
      let url =
        this.$CONSTANT.MY.saveEsShopUserCore +
        "?sessionId=" +
        this.$common.getCookie("sessionId");
      console.log(url);
      this.$common.post(
        url,
        {
          userId,
          weixinUserId: localStorage.getItem("weixinId")||'',
          type:this.$route.query.type == "store"?'':'special',
          openId:openId||''

        },
        res => {
          console.log(res);
        }
      );
    }
  },
  components: {}
};
</script>

<style lang="less">
#password {
  .top {
    height: 239px;
    // position: relative;
    .img1 {
      width: 100%;
      display: inline-block;
      height: 100%;
    }
    .img2 {
      position: absolute;
      width: 80px;
      height: 79px;
      top: 27px;
      left: 39.5%;
    }
  }
  .middle {
    margin: -106px 12px 0;
    border: 1px solid transparent;
    border-radius: 14px;
    box-shadow: 1px 1px 11px #d3cece;
    height: 350px;
    background: #fff;
    position: relative;
    .two {
      margin: 40px 20px 0;
      .section {
        height: 61px;
        line-height: 61px;
        position: relative;
        .img {
          display: inline-block;
          float: left;
          width: 17px;
          height: 17px;
          // margin-right: 8px;
          position: absolute;
          left: 9px;
          top: 4px;
          img {
            height: 100%;
          }
        }
        input {
          text-indent: 33px;
          // margin-left: -33px;
          padding: 20px 0;
          width: 100%;
          border-bottom: 1px solid #d9d9d9;
          font-family: PingFang-SC-Medium;
          font-size: 15px;
        }
        input::-webkit-input-placeholder {
          color: #999;
        }
      }
    }
    .three {
      margin-top: 109px;
      text-align: center;
      a {
        display: inline-block;
        width: 80%;
        height: 42px;
        line-height: 42px;
        border: 1px solid transparent;
        border-radius: 21px;
        background-color: #f02931;
        font-family: PingFang-SC-Medium;
        font-size: 17px;
        color: #fff;
        text-align: center;
        &:hover {
          background-color: #e1252c;
        }
      }
    }
  }
}
</style>
